<?php

/**
 * This is the model class for table "{{invoice}}".
 *
 * The followings are the available columns in table '{{invoice}}':
 * @property integer $id
 * @property integer $user_id
 * @property string $cost
 * @property string $update_time
 */
class Invoice extends CActiveRecord
{
	public $contract;
	public $username;
	public $number;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Invoice the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{invoice}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, cost, contract, username', 'required'),
			array('user_id, contract', 'numerical', 'integerOnly'=>true),
			array('cost', 'numerical'),
			array('update_time', 'unsafe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, contract, username, cost, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user'=>array(self::BELONGS_TO,'User','user_id',
					'select'=>'id,contract,username',),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('main','Faktura Number'),
			'user_id' => Yii::t('main','User'),
			'cost' => Yii::t('main','Cost'),
			'update_time' => Yii::t('main','Update Time'),
			'contract' => Yii::t('main','Contract'),
			'username' => Yii::t('main','Username'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria(array('with'=>array('user')));

		$criteria->compare('t.id',$this->id);

		$criteria->compare('user.contract',$this->contract);
		
		$criteria->compare('user.username',$this->username,true);

		$criteria->compare('t.cost',$this->cost,true);

		$criteria->compare('t.update_time',$this->update_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>Setting::get('PerPage')),
			'sort'=>array(
				'defaultOrder'=>'t.update_time DESC',
				'attributes'=>array(
					'contract'=>array(
						'asc'=>'user.contract',
						'desc'=>'user.contract DESC',
					),
					'username'=>array(
						'asc'=>'user.username',
						'desc'=>'user.username DESC',
					),
					'id',
					'cost',
					'update_time',
				),
			),
		));
	}
	
	public function getSum()
	{
		return sprintf("%.02f", $this->cost);
	}
	
	public function getWithoutPDV()
	{
		return sprintf("%.02f", $this->sum*5/6);
	}
	
	public function getWithPDV()
	{
		return sprintf("%.02f", $this->sum - $this->withoutPDV);
	}
	
	public function getNextId()
	{
		$sql="SHOW TABLE STATUS LIKE '{$this->tableName()}'";
		$command = Yii::app()->db->createCommand($sql);
		$row=$command->queryRow();
		return $row['Auto_increment'];
	}
}